
<!DOCTYPE html>
<html lang="">


<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">
  <meta name="theme-color" content="#202020"/>
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  <script src="/js/busuanzi.pure.mini.js" async></script>
  
  
    <meta name="keywords" content="UML,类图," />
  

  
    <meta name="description" content="一个专注一coding的网站。提供丰富编程知识，包括Spring、Java、EmberJS、SpringBoot等等技术。" />
  
  
  <link rel="icon" type="image/x-icon" href="/image/favicon.ico">
  <title>UML类图关系线 [ Keep Coding ]</title>
  
    <!-- stylesheets list from config.yml -->
    
      <link rel="stylesheet" href="/css/pure-min.css">
    
      <link rel="stylesheet" href="/css/xoxo.css">
    
  
<meta name="generator" content="Hexo 5.0.2"><link rel="alternate" href="/atom.xml" title="Keep Coding" type="application/atom+xml">
</head>

<body>
  <div class="nav-container">
    <nav class="home-menu pure-menu pure-menu-horizontal">
  <a class="pure-menu-heading" href="/">
    <img class="avatar" src="/image/favicon.ico">
    <span class="title">Keep Coding</span>
  </a>

  <ul class="pure-menu-list clearfix">
      
          
            <li class="pure-menu-item"><a href="/" class="pure-menu-link">首页</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/archives" class="pure-menu-link">归档</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/tags" class="pure-menu-link">标签</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/search" class="pure-menu-link">搜索</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/about" class="pure-menu-link">关于</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/atom.xml" class="pure-menu-link">订阅</a></li>
          
      
  </ul>
   
</nav>
  </div>

  <div class="container" id="content-outer">
    <div class="inner" id="content-inner">
      <div class="post-container">
  <article class="post" id="post">
    <header class="post-header text-center">
      <h1 class="title">
        UML类图关系线
      </h1>
      <span>
        
        <time class="time" datetime="2020-08-30T09:52:01.562Z">
        2020-08-30
      </time>
        
      </span>
      <span class="slash">/</span>
      <span class="post-meta">
      <span class="post-tags">
        <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/UML/" rel="tag">UML</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E7%B1%BB%E5%9B%BE/" rel="tag">类图</a></li></ul>
      </span>
    </span>
      <span class="slash">/</span>
      <span class="read">
      <span id="busuanzi_value_page_pv"></span> 点击
    </span>
      <span class="slash">/</span>
      <span class="read">阅读耗时 1 分钟</span>
    </header>

    <div class="post-content">
      <h1 id="类图关系线"><a href="#类图关系线" class="headerlink" title="类图关系线"></a>类图关系线</h1><h3 id="泛化关系（generalization）"><a href="#泛化关系（generalization）" class="headerlink" title="泛化关系（generalization）"></a>泛化关系（generalization）</h3><p>泛化关系使用带空心箭头的实线表示，比如汽车与SUV就是泛化关系；</p>
<p><img src="https://oscimg.oschina.net/oscnet/up-2f5c62b047a1a107f71948e25b35425157d.JPEG" alt="泛化关系"></p>
<blockquote>
<p>在代码的实现中，泛化关系表现为继承非抽象类。</p>
</blockquote>
<h3 id="实现关系（realize）"><a href="#实现关系（realize）" class="headerlink" title="实现关系（realize）"></a>实现关系（realize）</h3><p>实现关系使用空心箭头的虚线表示，比如抽象类和抽象类的实现类关系。</p>
<p><img src="https://oscimg.oschina.net/oscnet/up-20047ab524f492cba2b1d6e0019838e7ab4.JPEG" alt="实现关系"></p>
<blockquote>
<p>在代码的实线中，实现关系表现为继承抽象类（有别与接口的实现）。</p>
</blockquote>
<h3 id="聚合关系（aggregation）"><a href="#聚合关系（aggregation）" class="headerlink" title="聚合关系（aggregation）"></a>聚合关系（aggregation）</h3><p>聚合关系使用一条带空心菱形箭头的实线（没有箭头）表示。聚合关系用于表示对象之间的关系，表示整体由部分构成的语义，比如一个部门有多个员工构成。</p>
<p>与组合关系有所不同的是，整合和部分不是强依赖，即使整体不存在了，部分仍然存在，比如一个部门撤销了，员工不会消失。</p>
<p><img src="https://oscimg.oschina.net/oscnet/up-a0c81c84710766cc94a15809e51894ecbb5.JPEG" alt="聚合关系"></p>
<h3 id="组合关系（composition）"><a href="#组合关系（composition）" class="headerlink" title="组合关系（composition）"></a>组合关系（composition）</h3><p>组合关系使用一条实心菱形的实线（没有箭头）表示。与聚合关系一样，组合关系同样用来表示对象之间的关系，但是对象之间是强依赖的。如果整体不存在了那么部分也就不存在。比如公司不存在了那么公司里面的部门也就不存在了。</p>
<p><img src="https://oscimg.oschina.net/oscnet/up-36c473f94be51d7216e9337e98aa1b58c04.JPEG" alt="组合关系"></p>
<h3 id="关联关系（association）"><a href="#关联关系（association）" class="headerlink" title="关联关系（association）"></a>关联关系（association）</h3><p>关联关系使用一条带方向箭头的实线表示。它描述了不同类对象之间的关系，它是一种静态的关系，通常与运行状态无关，由一般常识等因素决定。关联关系是一种强依赖。</p>
<p>关联关系如果不强调方向（无指向），表对象相互知道（类相互引用）。如果强调方向需要带箭头，比如下图，A指向B，表示A依赖B，A知道B，但是B不依赖A，B不知道A。代码中非常常见，代码表现是B是A的一个成员属性（变量）。</p>
<p><img src="https://oscimg.oschina.net/oscnet/up-780dd5177fb080956943b3566252bf6de2b.JPEG" alt="关联关系"></p>
<blockquote>
<p>在代码中，关联关系通常是以成员变量的形式实现。</p>
</blockquote>
<h3 id="依赖关系（dependency）"><a href="#依赖关系（dependency）" class="headerlink" title="依赖关系（dependency）"></a>依赖关系（dependency）</h3><p>依赖关系是用一条带箭头 的虚线表示。它描述一个对象在运行期间会用到另外一个对象，比如方法的参数。</p>
<p>与关联关系不同的是，它是一种临时的关系，通常在运行期间产生，并且随着运行时的变化而变化（比如方法的参数是接口，会根据传不同的实现而变化），同时依赖关系也会发生变化。比如传的参数是实现类A变成实现类B。依赖关系由A类变成B类。</p>
<p>显然，依赖也是有双向的，双向依赖是一种非常糟糕的设计，我们总是应该保持单向依赖，杜绝双向依赖（比如类的循环依赖）。</p>
<p><img src="https://oscimg.oschina.net/oscnet/up-3bb64cf6806a0bc21998c0aa170b99f1a22.JPEG" alt="依赖关系"></p>
<blockquote>
<p>在代码实现中，依赖关系体现为类构造方法的以及普通的方法的传入参数，箭头的指向为调用关系（图中A的方法调用了B），依赖关系除了临时知道对方外，还是“使用”对方的方法和属性。</p>
</blockquote>

    </div>

      
    <div class="post-nav">
      <div class="post-nav-item post-nav-next">
        
          <span>〈 </span>
          <a href="/2020/08/02/Java/EvoSuite--%E8%87%AA%E5%8A%A8%E7%94%9F%E6%88%90%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95%E5%B7%A5%E5%85%B7/" rel="next" title="EvoSuite--自动生成单元测试工具">
          EvoSuite--自动生成单元测试工具
          </a>
        
      </div>
  
      <div class="post-nav-item post-nav-prev">
          
          <a href="/2020/08/30/Java/%E6%88%90%E5%91%98%E5%B1%9E%E6%80%A7%E3%80%81%E9%9D%99%E6%80%81%E5%B1%9E%E6%80%A7%E3%80%81%E6%9E%84%E9%80%A0%E5%99%A8%E5%8A%A0%E8%BD%BD%E9%A1%BA%E5%BA%8F/" rel="prev" title="类成员属性、静态变量、静态块、普通块、构造方法的加载顺序">
            类成员属性、静态变量、静态块、普通块、构造方法的加载顺序
          </a>
          <span>〉</span>
        
      </div>
    </div>
  
  </article>
  <div class="toc-container">
    
  <div id="toc" class="toc-article">
    <strong class="toc-title">目录</strong>
    <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%B1%BB%E5%9B%BE%E5%85%B3%E7%B3%BB%E7%BA%BF"><span class="toc-text">类图关系线</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B3%9B%E5%8C%96%E5%85%B3%E7%B3%BB%EF%BC%88generalization%EF%BC%89"><span class="toc-text">泛化关系（generalization）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9E%E7%8E%B0%E5%85%B3%E7%B3%BB%EF%BC%88realize%EF%BC%89"><span class="toc-text">实现关系（realize）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%81%9A%E5%90%88%E5%85%B3%E7%B3%BB%EF%BC%88aggregation%EF%BC%89"><span class="toc-text">聚合关系（aggregation）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%BB%84%E5%90%88%E5%85%B3%E7%B3%BB%EF%BC%88composition%EF%BC%89"><span class="toc-text">组合关系（composition）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%85%B3%E8%81%94%E5%85%B3%E7%B3%BB%EF%BC%88association%EF%BC%89"><span class="toc-text">关联关系（association）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BE%9D%E8%B5%96%E5%85%B3%E7%B3%BB%EF%BC%88dependency%EF%BC%89"><span class="toc-text">依赖关系（dependency）</span></a></li></ol></li></ol></li></ol>
  </div>


  </div>
</div>


<div class="copyright">
    <span>本作品采用</span>
    <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by/4.0/">知识共享署名 4.0 国际许可协议</a>
    <span>进行许可。 转载时请注明原文链接。</span>
</div>
<div class="share">

</div>
<div class="post-container">
    <article class="post">
      <div id="container"></div>
    </article>
</div>

<link rel="stylesheet" href="https://imsun.github.io/gitment/style/default.css">
<script src="https://imsun.github.io/gitment/dist/gitment.browser.js"></script>
<script>
var gitment = new Gitment({
  id: 'UML类图关系线', // 可选。默认为 location.href
  owner: 'ubuntuvim',
  repo: 'ubuntuvim.github.io',
  oauth: {
    client_id: '48b3df0bf4ba1743e0a3',
    client_secret: 'c6c2cec09acb5eecc9d325d56cef187264aa1e16',
  },
})
gitment.render('container')
</script>



    </div>

    

  </div>
  <footer class="footer text-center">
    <div id="bottom-inner">
        <a class="bottom-item" href="http://xcoding.tech/">首页</a> |
        <a class="bottom-item" href="http://xcoding.tech/" target="_blank">主站</a> |
        <a class="bottom-item" href="https://github.com/ubuntuvim" target="_blank">GitHub</a> |
        <a class="bottom-item" href="https://hexo.io" target="_blank">Powered by hexo</a> |
        <a class="bottom-item" href="https://github.com/KevinOfNeu/hexo-theme-xoxo" target="_blank">Theme xoxo</a>
    </div>
</footer>


  <script src='https://unpkg.com/mermaid@7.1.2/dist/mermaid.min.js'></script>
  <script>
    if (window.mermaid) {
      mermaid.initialize({theme: 'forest'});
    }
  </script>


  
  <!-- scripts list from theme config.yml -->
  
    <script src="/js/zepto.min.js"></script>
  


<script>
  (function(window, document, undefined) {

    var timer = null;

    function returnTop() {
      cancelAnimationFrame(timer);
      timer = requestAnimationFrame(function fn() {
        var oTop = document.body.scrollTop || document.documentElement.scrollTop;
        if (oTop > 0) {
          document.body.scrollTop = document.documentElement.scrollTop = oTop - 50;
          timer = requestAnimationFrame(fn);
        } else {
          cancelAnimationFrame(timer);
        }
      });
    }

    var hearts = [];
    window.requestAnimationFrame = (function() {
      return window.requestAnimationFrame ||
        window.webkitRequestAnimationFrame ||
        window.mozRequestAnimationFrame ||
        window.oRequestAnimationFrame ||
        window.msRequestAnimationFrame ||
        function(callback) {
          setTimeout(callback, 1000 / 60);
        }
    })();
    init();

    function init() {
      css(".heart{z-index:9999;width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: absolute;}.heart:after{top: -5px;}.heart:before{left: -5px;}");
      attachEvent();
      gameloop();
      addMenuEvent();
    }

    function gameloop() {
      for (var i = 0; i < hearts.length; i++) {
        if (hearts[i].alpha <= 0) {
          document.body.removeChild(hearts[i].el);
          hearts.splice(i, 1);
          continue;
        }
        hearts[i].y--;
        hearts[i].scale += 0.004;
        hearts[i].alpha -= 0.013;
        hearts[i].el.style.cssText = "left:" + hearts[i].x + "px;top:" + hearts[i].y + "px;opacity:" + hearts[i].alpha + ";transform:scale(" + hearts[i].scale + "," + hearts[i].scale + ") rotate(45deg);background:" + hearts[i].color;
      }
      requestAnimationFrame(gameloop);
    }

    /**
     * 给logo设置点击事件
     * 
     * - 回到顶部
     * - 出现爱心
     */
    function attachEvent() {
      var old = typeof window.onclick === "function" && window.onclick;
      var logo = document.getElementById("logo");
      if (logo) {
        logo.onclick = function(event) {
          returnTop();
          old && old();
          createHeart(event);
        }
      }
      
    }

    function createHeart(event) {
      var d = document.createElement("div");
      d.className = "heart";
      hearts.push({
        el: d,
        x: event.clientX - 5,
        y: event.clientY - 5,
        scale: 1,
        alpha: 1,
        color: randomColor()
      });
      document.body.appendChild(d);
    }

    function css(css) {
      var style = document.createElement("style");
      style.type = "text/css";
      try {
        style.appendChild(document.createTextNode(css));
      } catch (ex) {
        style.styleSheet.cssText = css;
      }
      document.getElementsByTagName('head')[0].appendChild(style);
    }

    function randomColor() {
      // return "rgb(" + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + ")";
      return "#F44336";
    }

    function addMenuEvent() {
      var menu = document.getElementById('menu-main-post');
      if (menu) {
        var toc = document.getElementById('toc');
        if (toc) {
          menu.onclick = function() {
            if (toc) {
              if (toc.style.display == 'block') {
                toc.style.display = 'none';
              } else {
                toc.style.display = 'block';
              }
            }
          };
        } else {
          menu.style.display = 'none';
        }
      }
    }

  })(window, document);
</script>

  



</body>
</html>
